<?php
namespace Admin\Controller;
use Think\Controller;
class LogsController extends Controller {
	public function index(){
		$name=session('admin.username');
		$id=M("admin")->where("username='{$name}'")->getField("id");//获取id
		$group_id=M("auth_group_access")->where("uid='{$id}'")->getField("group_id");//获取用户组id
		//搜索条件
		$where=array();
        if(!empty($_GET['module'])){
            $where['module']=array('like',"%{$_GET['module']}%");
        }
        $begintime=$_GET['begintime'];
        $endtime=$_GET['endtime'];
        if(!empty($begintime) && !empty($endtime) ){
            $where['time']=array(array( 'GT', strtotime($begintime)),array( 'LT', strtotime($endtime)));
        }
       	if(!empty($begintime) && empty($endtime)){
            $where['time']=array( 'GT', strtotime($begintime));
        }
        if(empty($begintime) && !empty($endtime)){
            $where['time']=array( 'LT', strtotime($endtime));
        }
        //判断用户是否是超管 查询操作日志 
        if($group_id !==1){
        	$where['userid']=$id;
        }
		$mod=M("admin_log");

		//实例化分页类
        $page=new \Think\Page($mod->where($where)->Count(),10);
        //分页设置
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('first','首页');
        $page->setConfig('last','末页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $logs=$mod->where($where)->limit($page->firstRow,$page->listRows)->order('time desc')->select();
		$this->assign('page',$page->show());//分页显示
		$this->assign("logs",$logs);
		$this->display();
	}

	//清空日志
	public function del(){
		//判断日志信息是否存在
		$a=M("admin_log")->select();
		if($a){
			//使数据表id从1开始自增
			$mods=M();
			$sql="TRUNCATE TABLE xwcms_admin_log";
			$mods->execute($sql);
			$mod=M("admin_log")->where("1")->delete();
			$this->success("清除日志成功",U("Logs/index"));
		}else{
			$this->success("清除日志成功",U("Logs/index"));
		}
	}

	//删除操作日志
	public function dodel(){
		//获取选中操作日志的id
		$a= $_GET;
		foreach ($a as $key=>$value){
			foreach ($value as $k=>$v){
				$mod=M("admin_log")->delete($v);
			}	
		}
		echo  1;
	}
	
}